package com.fingard.dsp.bank.directbank.vbao02.util;
import java.security.MessageDigest;

public class CryptTool {
	/**
	 * MD5 摘要计算(byte[]).
	 *
	 * @param src
	 *            byte[]
	 * @throws java.lang.Exception
	 * @return byte[] 16 bit digest
	 */
	public static byte[] md5Digest(byte[] src) throws Exception {
		MessageDigest alg = MessageDigest.getInstance("MD5");
		return alg.digest(src);
	}

	/**
	 * SHA-1 摘要计算
	 * 
	 * @param src
	 * @return
	 * @throws Exception
	 */
	public static byte[] sha1Digest(byte[] src) throws Exception {
		MessageDigest alg = MessageDigest.getInstance("SHA-1");
		return alg.digest(src);
	}

	/**
	 * MD5 摘要计算(String).
	 *
	 * @param src
	 *            String
	 * @throws java.lang.Exception
	 * @return String
	 */
	public static String md5Digest(String src) throws Exception {
		return byteToHex(md5Digest(src.getBytes("UTF-8")));
	}

	/**
	 * SHA1 摘要算法（String）
	 * 
	 * @param src
	 * @return
	 * @throws Exception
	 */
	public static String sha1Digest(String src) throws Exception {
		return byteToHex(sha1Digest(src.getBytes()));
	}

	public static String byteToHex(byte[] ibArray) {
		String idStr = "";
		for (int i = 0; i < ibArray.length; i++) {
			idStr += byteHEX(ibArray[i]);
		}
		return idStr;
	}

	public static String byteHEX(byte ib) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
				'b', 'c', 'd', 'e', 'f' };
		char[] ob = new char[2];
		ob[0] = Digit[(ib >>> 4) & 0X0F];
		ob[1] = Digit[ib & 0X0F];
		String s = new String(ob);
		return s;
	}

	public static byte[] hexString2Bytes(String str) {
		byte[] b = new byte[str.length() / 2];
		for (int i = 0; i < b.length; i++) {
			String s = str.substring(i * 2, i * 2 + 2);
			b[i] = hex2Byte(s);
		}
		return b;
	}

	public static byte hex2Byte(String str) {
		String seed = "0123456789abcdef";
		return (byte) (seed.indexOf(str.substring(0, 1)) * 16 + seed
				.indexOf(str.substring(1, 2)));
	}

	/**
	 * 特殊检验算法
	 * 
	 * @param src
	 * @return
	 */
	public static String getCheckData(String src) {
		byte[] byteArray = src.getBytes();
		int j = byteArray.length / 8 + 1;
		byte[] macArray = new byte[8];
		for (int i = 0; i < macArray.length; i++)
			macArray[i] = (byte) ("0".charAt(0));
		byte[] dataValue = new byte[j * 8];
		for (int i = 0; i < dataValue.length; i++)
			dataValue[i] = (byte) ("0".charAt(0));
		System.arraycopy(byteArray, 0, dataValue, 0, byteArray.length);
		for (int i = 0; i < j; i++) {
			byte[] tempValue = new byte[8];
			for (int m = 0; m < 8; m++) {
				tempValue[m] = dataValue[i * 8 + m];
			}
			getXorValue(macArray, tempValue);
		}
		return byteToHex(macArray);
	}

	private static void getXorValue(byte[] macArray, byte[] tempValue) {
		int i;
		for (i = 0; i < 8; i++)
			macArray[i] ^= tempValue[i];
	}

	/**
	 * 按字节取反
	 * 
	 * @param buff
	 * @return
	 */
	public static byte[] change(byte[] buff) {
		for (int i = 0; i < buff.length; i++) {
			int b = 0;
			for (int j = 0; j < 8; j++) {
				int bit = (buff[i] >> j & 1) == 0 ? 1 : 0;
				b += (1 << j) * bit;
			}
			buff[i] = (byte) b;
		}
		return buff;
	}

	/**
	 * 左加补零算法
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String AddLeft0(String str, int length) {
		if (str == null)
			str = "";
		boolean haveSign = false;
		if (!str.equalsIgnoreCase("") && str.substring(0, 1).equals("-")) {
			length = length - 1;
			str = str.substring(1);
			haveSign = true;
		}
		str = str.trim();
		while (str.length() < length)
			str = "0" + str;
		if (haveSign)
			str = "-" + str;
		return str;
	}

	public static void main(String[] args) throws Exception {
		/*
		 * String str="4354"; String myinfo=
		 * "0200|010800004993|1002|733101|20040920|9001|252525|||20040202|20041022|000000000000|000000000000|000000000000|0108|7331010245|||AAAAAAA|||3310130201|4621||||"
		 * ; System.out.println("本信息摘要是:"+CryptTool.getCheckData(myinfo)); str =
		 * "0200|1308897997131374000|1001|10000000037|20110624 144637|9008|2010122705|||||||||0||||0||5000004001|||||"
		 * ; System.out.println(CryptTool.md5Digest(str));
		 * System.out.println(CryptTool.sha1Digest(str)); //System.in.read();
		 * 
		 * String str1="123456"; System.out.println(str1.getBytes());
		 * System.out.println(CryptTool.hexString2Bytes(str1));
		 * System.out.println
		 * (CryptTool.byteToHex(CryptTool.hexString2Bytes(str1)));
		 * 
		 * System.out.println(CryptTool.md5Digest("a"));
		 * System.out.println(CryptTool.md5Digest("A"));
		 * 
		 * String str="12ab"; byte[] a = change(hexString2Bytes(str));
		 * System.out.println("a="+byteToHex(a));
		 */

		byte[] a = CryptTool.hexString2Bytes("2014010010000001");
		byte[] b = "2014010010000001".getBytes();
		System.out.println(a);
		System.out.println(b);
	}
}
